//
//  NetworkClient+Orders.swift
//  SitPosition
//
//  Created by apple on 2019/2/15.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation
import Alamofire


/**
 * 订单相关的网络接口
 */
extension NetworkApi {
    
    // 查询产品价格
    public static func productPrice() -> NetworkApi {
        return NetworkApi("/setup/setups/productFee", method: .get)
    }
    
    // 查询订单
    public static func userOrders() -> NetworkApi {
        return NetworkApi("/userCenter/userOrders", method: .get)
    }
    
    // 支付订单
    public static func payOrder(_ placeholder: Placeholder) -> NetworkApi {
        return NetworkApi("/userCenter/userOrders/{id}/action/payOrder",
                          method: .patch,
                          placeholder: placeholder)
    }
    
}


/**
 * 订单相关的网络请求
 */
extension NetworkClient {
    
    // 查询产品价格
    func productPrice(success: DataParamCallback?,
                      failure: ErrorCallback?) {
        let completionHandler = commonDataRequestCompletion(success: success, failure: failure)
        NetworkClient.instance.request(NetworkApi.productPrice(), params: nil, headers: nil).responseData(completionHandler: completionHandler)
    }
    
    // 查询订单
    func userOrders(success: DataParamCallback?,
                    failure: ErrorCallback?) {
        let completionHandler = commonDataRequestCompletion(success: success, failure: failure)
        NetworkClient.instance.request(NetworkApi.userOrders(), params: nil, headers: nil).responseData(completionHandler: completionHandler)
    }
    
    // 支付订单
    func payOrder(id: String,
                  payMethod: PurchaseType,
                  success: DataParamCallback?,
                  failure: ErrorCallback?) {
        let params: Parameters = [
            "payMethod" : payMethod.rawValue
        ]
        let placeholder: Placeholder = [
            "id": id
        ]
        let completionHandler = commonDataRequestCompletion(success: success, failure: failure)
        NetworkClient.instance.request(NetworkApi.payOrder(placeholder), params: params, headers: nil).responseData(completionHandler: completionHandler)
    }
    
}
